package com.example.gridlayoutdemo08.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.GridLayout;
import android.widget.TextView;

import com.example.gridlayoutdemo08.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wanghao on 2016/5/25.
 */
public class DragDialog extends Dialog {

    private DragGridLayout mShowGrid;//显示表格的布局
    private DragGridLayout mHideGrid;//隐藏表格的布局

    private DragDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private DragDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }
    public DragDialog(Context context) {
        this(context,R.style.dialog_style);

   //     Dialog dialog = new Dialog(MainActivity.this, R.style.dialog_style);
        WindowManager.LayoutParams params = this.getWindow().getAttributes();
        params.height = GridLayout.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.TOP;
        this.setContentView(R.layout.dialog);
        this.setCanceledOnTouchOutside(true);

        //获取到对应的显示与隐藏的gridlayout
        mShowGrid = (DragGridLayout) findViewById(R.id.showGrid);
        mShowGrid.setAllowDrag(true);
        mHideGrid = (DragGridLayout) findViewById(R.id.hindGrid);
        mHideGrid.setAllowDrag(false);
        mShowGrid.setOnClickItemListener(new DragGridLayout.OnClickItemListener() {
            @Override
            public void onClickItem(TextView tv) {
                mShowGrid.removeView(tv);
                mHideGrid.addItem(tv.getText().toString());
            }
        });
        mHideGrid.setOnClickItemListener(new DragGridLayout.OnClickItemListener() {
            @Override
            public void onClickItem(TextView tv) {
                mHideGrid.removeView(tv);
                mShowGrid.addItem(tv.getText().toString());
            }
        });
    }
    //向外提供一个设置显示与隐藏的数据集合的set方法
    public void setShowAndHideItems(List<String> showItems,List<String> hideItems){
        if (showItems!=null && showItems.size() > 0){
            mShowGrid.setItems(showItems);
        }
        if (hideItems != null && hideItems.size() >0){
            mHideGrid.setItems(hideItems);
        }
    }
    //返回所有显示的频道的文本集合
    public List<String> getShowItems() {
        List<String> showItems = new ArrayList<>();
        for (int i = 0; i < mShowGrid.getChildCount(); i++) {
            View childAt = mShowGrid.getChildAt(i);
            showItems.add(((TextView) childAt).getText().toString());
        }
        return showItems;
    }
}
